package com.golddaymicromall.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.golddaymicromall.activity.R;
import com.golddaymicromall.activity.recharge.ZaiXianHuiKuanActivity;
import com.golddaymicromall.adapter.OnlineRemittanceAdapter;
import com.golddaymicromall.bean.OnlineRemittanceInfo;
import com.golddaymicromall.module.ListViewLoadMore;
import com.golddaymicromall.util.Log;
import com.golddaymicromall.util.RopUtils;
import com.golddaymicromall.util.SPUtils;
import com.golddaymicromall.util.ServerConfig;
import com.google.common.collect.Maps;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.convert.StringConvert;
import com.lzy.okrx.RxAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;

/**
 * Created by 王立强 on 2016/3/2.
 */
public class ZaiXianHuiKuanFragment extends Fragment implements ListViewLoadMore.IsLoadingListener, View.OnClickListener {
    private View view;
    private ListViewLoadMore mOnlineRemittance;
    private OnlineRemittanceAdapter onlineRemittanceAdapter;
    private List<OnlineRemittanceInfo> listOnlineInfo = new ArrayList<OnlineRemittanceInfo>();
    private List<OnlineRemittanceInfo> listAll = new ArrayList<OnlineRemittanceInfo>();
    private Handler handler = new Handler();
    private int CurrentPageNum1 = 0;
    private int flag = 1;
    private TextView mzaixianhuikuan_btn;
    private String logid;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_zaixianhuikuan, container, false);
        logid = SPUtils.get(getContext(), "Id", "").toString();
        initView();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.e("onResume()","onResume()");
        CurrentPageNum1 = 1;
        attemptGetOnlineRemittance(CurrentPageNum1);
//        onlineRemittanceAdapter.notifyDataSetChanged();
    }

    private void initView() {
        mzaixianhuikuan_btn = (TextView) view.findViewById(R.id.zaixianhuikuan_btn);
        mzaixianhuikuan_btn.setOnClickListener(this);
        mOnlineRemittance = (ListViewLoadMore) view.findViewById(R.id.recharge_zaixianhuikuan);
        mOnlineRemittance.setOnLoadingListener(this);
    }



    public void attemptGetOnlineRemittance(int CurrentPageNum) {

        Map<String, String> form = Maps.newHashMap();
        form.put("method", "rop.onlineremittance.get");
        form.put("appKey", "00001");
        form.put("v", "1.0");
        form.put("format", "json");
        form.put("customerid", logid);
        form.put("current_pagenum", String.valueOf(CurrentPageNum));
        String sing = RopUtils.signString(form, "qwertyuiop");
        form.put("sign", sing);

        OkGo.post(new ServerConfig().SERVER)
                .params(form)
                .getCall(StringConvert.create(), RxAdapter.<String>create())
                .observeOn(AndroidSchedulers.mainThread())

                .subscribe(result -> {
                    Log.e("result",result);
                    if (!"".equals(result)) {
                        if (result.contains("errorToken") || "0".equals(result)) {
                            Toast.makeText(getActivity(), "访问服务器失败,请稍后重试", Toast.LENGTH_SHORT).show();
                        } else if ("null".equals(result)) {
                        } else {
                            listOnlineInfo = JSON.parseArray(result, OnlineRemittanceInfo.class);
                            if (CurrentPageNum > 1) {
                                listAll.addAll(listOnlineInfo);
                            } else if (CurrentPageNum == 1) {
                                listAll.clear();
                                listAll.addAll(listOnlineInfo);
                            }
                            onlineRemittanceAdapter = new OnlineRemittanceAdapter(listAll, getActivity());
                            mOnlineRemittance.setAdapter(onlineRemittanceAdapter);
                            onlineRemittanceAdapter.notifyDataSetChanged();
                            if (CurrentPageNum > 1) {
                                mOnlineRemittance.setSelection(mOnlineRemittance.getCount() - 1);
                            }
                        }
                    } else {
                        Toast.makeText(getActivity(), "没有请求到数据,请稍后重试", Toast.LENGTH_SHORT).show();
                    }
                },throwable -> {
                   throwable.printStackTrace();
                });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.zaixianhuikuan_btn:
                Intent intent = new Intent(getActivity(), ZaiXianHuiKuanActivity.class);

                startActivity(intent);
                break;
        }
    }

    @Override
    public void onLoad() {

        Observable.timer(3, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(aLong -> {
                    CurrentPageNum1 = CurrentPageNum1 + 1;
                    attemptGetOnlineRemittance(CurrentPageNum1);
                    onlineRemittanceAdapter.notifyDataSetChanged();
                    mOnlineRemittance.complateLoad();
                });
    }
}
